package eppic.pkg;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.app.*;
import android.content.*;
import android.widget.Toast;
import 	android.view.LayoutInflater;
import 	android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;





public class ProjectsForActivity extends Activity {
	
	public Project[] projects;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.projectfor);
        
        ViewGroup project_box = (ViewGroup) findViewById(R.id.projects);

        //Get CLient Id
        Bundle extras = getIntent().getExtras();
        int id = extras.getInt("client_id");
        
        // Get all  Projects for Client_Id
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        projects = dbHelper.getAllProjects(id);
        
        //Populate Projects
        if(projects.length > 0)
        {
        	for(int i = 0; i < projects.length; i++)
        	{
        		Project x = projects[i];
            	
            	Button button = new Button(this);
    	        button.setText(x.name);
    	        button.setOnClickListener(launch_projectMenu);
    	        button.setTag(i);
    	        project_box.addView(button);
        	}

        }
        
       
    }
    
  //Launch Project Menu
    public View.OnClickListener launch_projectMenu = new View.OnClickListener()
    {
    	 public void onClick(View v) {
    		 
    		 projectMenu((Integer) v.getTag());
    		 
         }
    
    };
    
    //Create Project Menu
    public void projectMenu(int id)
    {
    	final CharSequence[] items = {"Edit", "Clock In", "Clock in at...", "Delete"};
    	final Project project = projects[id];
    	
    	AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setItems(items, new DialogInterface.OnClickListener() {
       	    public void onClick(DialogInterface dialog, int item) {
       	    	if(items[item] == "Delete")
       	    	{
       	    		String toastMsg;
       	    		if(project.delete(ProjectsForActivity.this))
       	    		{
       	    			toastMsg = "Project Deleted";
       	    			reloadList();
       	    		}
       	    		else
       	    		{
       	    			toastMsg = "Failed to delete project. Try again.";
       	    		}
       	    		Toast.makeText(getApplicationContext(), toastMsg, Toast.LENGTH_SHORT).show();
       	    	}
       	    	if(items[item] == "Edit")
       	    	{
       	    		Intent i = new Intent(ProjectsForActivity.this, EditProjectActivity.class);
       	    		i.putExtra("project_id", project.id);
       	    		startActivityForResult(i, 1);
       	    	}
       	    }
       	});
        AlertDialog alert = alt_bld.create();
        // Title for AlertDialog
        alert.setTitle(project.name);
        alert.show();
    }
    
    public void goBack(View view) {
    	finish();
    }
   

    public void reloadList()
    {
	   	 Intent i = getBaseContext().getPackageManager()
	   	 .getLaunchIntentForPackage(getBaseContext().getPackageName() );
	
	   	 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
	   	 startActivity(i);
    }
    
    protected void onActivityResult (int requestCode, int resultCode, Intent data)
    {	 
    	reloadList();
    }
    
}